from datetime import datetime
from typing import Optional

class Item:
    """商品数据模型"""
    
    def __init__(self, id: int, name: str, description: str, price: float, 
                 category: str, created_at: Optional[datetime] = None, 
                 updated_at: Optional[datetime] = None):
        self.id = id
        self.name = name
        self.description = description
        self.price = price
        self.category = category
        self.created_at = created_at or datetime.now()
        self.updated_at = updated_at or datetime.now()
    
    def update(self, **kwargs):
        """更新商品信息"""
        for key, value in kwargs.items():
            if hasattr(self, key) and value is not None:
                setattr(self, key, value)
        self.updated_at = datetime.now()
    
    def to_dict(self):
        """转换为字典格式"""
        return {
            "id": self.id,
            "name": self.name,
            "description": self.description,
            "price": self.price,
            "category": self.category,
            "created_at": self.created_at,
            "updated_at": self.updated_at
        }
    
    def __repr__(self):
        return f"<Item(id={self.id}, name='{self.name}', category='{self.category}')>" 